/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.isfd.constant.enums.IsfdMetadataTableNameEnum;
import com.comac.ins.isfd.domain.IsfdLoad;
import com.comac.ins.isfd.domain.bo.IsfdLoadBo;
import com.comac.ins.isfd.domain.bo.query.IsfdLoadQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdLoadVo;
import com.comac.ins.isfd.mapper.IsfdLoadMapper;
import com.comac.ins.isfd.service.IIsfdLoadService;
import com.comac.ins.isfd.service.IIsfdMetadataImportService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;

/**
 * 载荷库功能Service业务层处理
 *
 * @author lanlan
 * @date 2024-07-16
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class IsfdLoadServiceImpl implements IIsfdLoadService, IIsfdMetadataImportService {

    private final IsfdLoadMapper baseMapper;

    /**
     * 查询载荷库功能
     *
     * @param no 主键
     * @return 载荷库功能
     */
    @Override
    public IsfdLoadVo queryById(Integer no) {
        return baseMapper.selectVoById(no);
    }

    /**
     * 分页查询载荷库功能列表
     *
     * @param bo 查询条件
     * @return 载荷库功能分页列表
     */
    @Override
    public TableDataInfo<IsfdLoadVo> queryPageList(IsfdLoadQueryBo bo) {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageNum(bo.getPageNum());
        pageQuery.setPageSize(bo.getPageSize());

        LambdaQueryWrapper<IsfdLoad> lqw = buildQueryWrapper(bo);
        Page<IsfdLoadVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的载荷库功能列表
     *
     * @param bo 查询条件
     * @return 载荷库功能列表
     */
    @Override
    public List<IsfdLoadVo> queryList(IsfdLoadQueryBo bo) {
        LambdaQueryWrapper<IsfdLoad> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdLoad> buildQueryWrapper(IsfdLoadQueryBo bo) {
        LambdaQueryWrapper<IsfdLoad> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getNo() != null, IsfdLoad::getNo, bo.getNo());
        lqw.eq(bo.getG() != null, IsfdLoad::getG, bo.getG());
        lqw.eq(bo.getX() != null, IsfdLoad::getX, bo.getX());
        lqw.eq(bo.getY() != null, IsfdLoad::getY, bo.getY());
        lqw.eq(bo.getZ() != null, IsfdLoad::getZ, bo.getZ());
        lqw.eq(bo.getIxx() != null, IsfdLoad::getIxx, bo.getIxx());
        lqw.eq(bo.getIyy() != null, IsfdLoad::getIyy, bo.getIyy());
        lqw.eq(bo.getIzz() != null, IsfdLoad::getIzz, bo.getIzz());
        lqw.eq(bo.getIxy() != null, IsfdLoad::getIxy, bo.getIxy());
        lqw.eq(bo.getIxz() != null, IsfdLoad::getIxz, bo.getIxz());
        lqw.eq(bo.getIyz() != null, IsfdLoad::getIyz, bo.getIyz());
        lqw.in(CollectionUtil.isNotEmpty(bo.getMetadataManagementIds()), IsfdLoad::getMetadataManagementId, bo.getMetadataManagementIds());
        return lqw;
    }

    /**
     * 新增载荷库功能
     *
     * @param bo 载荷库功能
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdLoadBo bo) {
        IsfdLoad add = MapstructUtils.convert(bo, IsfdLoad.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setNo(add.getNo());
        }
        return flag;
    }

    /**
     * 修改载荷库功能
     *
     * @param bo 载荷库功能
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdLoadBo bo) {
        IsfdLoad update = MapstructUtils.convert(bo, IsfdLoad.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdLoad entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除载荷库功能信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String selectByTableName() {
        return IsfdMetadataTableNameEnum.ISFD_LOAD.getValue();
    }

    @Override
    public Long excelImport(File excelImportFile, String metadataManagementId) {
        long successCount = 0L;

        try (InputStream is = new FileInputStream(excelImportFile)) {
            List<IsfdLoadVo> targetVo = ExcelUtil.importExcel(is, IsfdLoadVo.class);
            for (IsfdLoadVo vo : targetVo) {
                IsfdLoadBo bo = MapstructUtils.convert(vo, IsfdLoadBo.class);
                try {
                    bo.setMetadataManagementId(metadataManagementId);
                    this.insertByBo(bo);
                    successCount++;
                } catch (Exception e) {
                    log.error("载荷库数据导入失败", e);
                }
            }

        } catch (Exception e) {
            log.error("sheet文件转换为实体类失败", e);
            throw new BaseException("sheet文件转换为实体类失败");
        }

        return successCount;
    }
}
